package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.collections.ArrayCollection;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetActionsEvent;
	import net.optix.ip.model.ModelLocator;
	import net.optix.ip.vo.ActionVO;
	
	public class GetActionsCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var myEvent:GetActionsEvent = GetActionsEvent( event );
			
			var delegate:InventionDelegate = new InventionDelegate(  );     
			delegate.getActions( this , myEvent.inventionID );
			
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			//trace(event.result);
			try
			{
				model.actions	=	event.result.actions[0];
				
				var actions:XML						=	event.result.officeActions[0];
				var officeActions:ArrayCollection	=	new ArrayCollection();
				
				var action1:int	=	-1;
				var action2:int	=	-1;
				var action:ActionVO;
				
				for(var i:int=0; i<actions.Data.length(); i++)
				{
					action	=	ActionVO.purseAction(actions.Data[i]);
					if(action.officeActionTypeInt == 2)
					{
						action1	=	i;
					}
					if(action.responseType == 4)
					{
						action2	=	i;
					}
					officeActions.addItem(action);
				}
				trace(action1+"  action1  "+action2+"");
				if(action1 > -1 && action2 > -1 && action1 != action2)
				{
					action	=	officeActions[action2];
					officeActions[action2]	=	officeActions[action1];
					officeActions[action1]	=	action;
				}
				model.officeActions	=	officeActions;
				
				
			}
			catch(error:Error){}
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			
		}

	}
}